import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;
import java.util.logging.Logger;
import javax.swing.SwingUtilities;
import org.rsbot.event.events.MessageEvent;
import org.rsbot.event.listeners.MessageListener;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.methods.Bank;
import org.rsbot.script.methods.Calculations;
import org.rsbot.script.methods.Camera;
import org.rsbot.script.methods.Environment;
import org.rsbot.script.methods.Equipment;
import org.rsbot.script.methods.Game;
import org.rsbot.script.methods.GrandExchange;
import org.rsbot.script.methods.GrandExchange.GEItem;
import org.rsbot.script.methods.GroundItems;
import org.rsbot.script.methods.Interfaces;
import org.rsbot.script.methods.Inventory;
import org.rsbot.script.methods.Keyboard;
import org.rsbot.script.methods.Magic;
import org.rsbot.script.methods.Menu;
import org.rsbot.script.methods.Mouse;
import org.rsbot.script.methods.NPCs;
import org.rsbot.script.methods.Objects;
import org.rsbot.script.methods.Skills;
import org.rsbot.script.methods.Walking;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSGroundItem;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSItem;
import org.rsbot.script.wrappers.RSNPC;
import org.rsbot.script.wrappers.RSObject;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;

@ScriptManifest(authors={"WhiteBear"}, keywords={"Magic All in One"}, name="White Bear AIO Magic", version=2.11D, description="Flawless All-in-One script for training magic!", website="http://whitebearrs.orgfree.com")
public class WhiteBearAIOMagic extends Script
  implements PaintListener, MessageListener, MouseListener, MouseMotionListener
{
  final ScriptManifest properties;
  WhiteBearAIOMagic.WhiteBearPaint thePainter;
  WhiteBearAIOMagic.ChatResponder chatRes;
  WhiteBearAIOMagic.Antiban antiban;
  private final Properties WBini;
  private int lvlAmt;
  private int minMS;
  private int maxMS;
  private int priceGuide;
  private int bankCount;
  private int exitStage;
  private int camTurned;
  private int resCount;
  private int midTime;
  private int randTime;
  private int midLength;
  private int randLength;
  private int counter;
  private int relogAfter;
  private int itemID;
  private int npcID;
  private int bankMode;
  private int doWhat;
  private int component;
  private int pastExp;
  private int count1;
  private int curseComponent;
  private int barID;
  private int ore1ID;
  private int ore2ID;
  private int ore1Amt;
  private int ore2Amt;
  private int foodItem;
  private int bolteId;
  private int boltComponent;
  private int floor;
  private double totalExp;
  private double exp;
  private double expGained;
  private final double curseExpGained = 0.0D;
  private long stopTime;
  private long nextBreak;
  private long nextLength;
  private long endTime;
  private long nextRun;
  private long curseWait;
  private boolean unactivated;
  private boolean foundType;
  private boolean breakLogout;
  private boolean logOutInfo;
  private boolean tradeResponse;
  private boolean doBanking;
  private boolean invFull;
  private boolean useChatRes;
  private boolean useFkeys;
  private boolean guiStart;
  private boolean chatResGUI;
  private boolean useBreaking;
  private boolean randomBreaking;
  private boolean checkUpdates;
  private boolean currentlyBreaking;
  private boolean useRemote;
  private boolean doingRemote;
  private boolean doRelog;
  private boolean logOutR;
  private boolean clicked;
  private String totalTime;
  private String itemtype;
  private String spellName;
  private String barName;
  private String remoteName;
  private String remoteMsg;
  private String remoteReply;
  private String status;
  private String colour;
  private String lastMessage;
  private RSTile startLoc;

  public WhiteBearAIOMagic()
  {
    this.properties = ((ScriptManifest)getClass().getAnnotation(ScriptManifest.class));

    this.thePainter = new WhiteBearAIOMagic.WhiteBearPaint(this, null);

    this.antiban = new WhiteBearAIOMagic.Antiban(this, null);
    this.WBini = new Properties();

    this.lvlAmt = 0; this.minMS = 4; this.maxMS = 6;
    this.priceGuide = 1; this.bankCount = 0;
    this.exitStage = 0; this.camTurned = 0; this.resCount = 0;
    this.midTime = 90; this.randTime = 120; this.midLength = 10; this.randLength = 20;
    this.counter = 0; this.relogAfter = -1; this.itemID = -1; this.npcID = -1; this.bankMode = 0;

    this.doWhat = -1; this.component = -1; this.pastExp = 0; this.count1 = 0; this.curseComponent = -1;

    this.barID = 0; this.ore1ID = 0; this.ore2ID = 0; this.ore1Amt = 0; this.ore2Amt = 0; this.foodItem = 0; this.bolteId = -1; this.boltComponent = -1; this.floor = 0;

    this.totalExp = 0.0D; this.exp = 0.0D; this.expGained = 0.0D;
    this.curseExpGained = 0.0D;
    this.stopTime = -1L; this.nextBreak = System.currentTimeMillis(); this.nextLength = 60000L;

    this.endTime = System.currentTimeMillis(); this.nextRun = System.currentTimeMillis(); this.curseWait = System.currentTimeMillis();

    this.unactivated = true; this.foundType = false; this.breakLogout = false;
    this.logOutInfo = false; this.tradeResponse = false; this.doBanking = false; this.invFull = false;

    this.useChatRes = true; this.useFkeys = true; this.guiStart = false; this.chatResGUI = false;

    this.useBreaking = false; this.randomBreaking = false; this.checkUpdates = false; this.currentlyBreaking = false;

    this.useRemote = false; this.doingRemote = false; this.doRelog = false; this.logOutR = false; this.clicked = false;

    this.totalTime = "00:00:00"; this.itemtype = "Unknown"; this.spellName = ""; this.barName = "null";

    this.remoteName = ""; this.remoteMsg = ""; this.remoteReply = ""; this.status = "Loading..."; this.lastMessage = null;

    this.startLoc = null;
  }

  public int loop()
  {
    try
    {
      this.mouse.setSpeed(random(this.minMS, this.maxMS));
      if (this.unactivated) {
        activate();
        this.unactivated = false;
      }
      if (this.game.getClientState() != 10) {
        this.interfaces.getComponent(976, 6).doClick();
        this.env.enableRandom("Login");
        return random(50, 100);
      }
      this.env.disableRandom("Login");
      if (this.counter == 398) {
        this.env.saveScreenshot(false);
        this.counter = 397;
      }
      if ((this.logOutR) || (this.exitStage == 2)) {
        doLogOut(false, true);
        return 1000;
      }
      if ((this.stopTime > 0L) && (System.currentTimeMillis() > this.stopTime)) {
        log("Stop Time Reached. Logging off in 5 seconds.");
        sleep(random(4950, 5600));
        doLogOut(false, true);
      }
      if (this.doingRemote) {
        if (this.doRelog) {
          this.endTime = (System.currentTimeMillis() + this.relogAfter * 60 * 1000);

          doLogOut(false, false);
          while (this.endTime > System.currentTimeMillis()) {
            sleep(100);
          }
          this.doingRemote = false;
        } else {
          doLogOut(false, true);
          return 100;
        }
      }
      if ((WhiteBearAIOMagic.Antiban.access$200(this.antiban)) && (this.useBreaking)) {
        long l = System.currentTimeMillis() + this.nextLength;
        this.currentlyBreaking = true;
        if (this.breakLogout) {
          doLogOut(true, false);
        }
        log("Taking a break for " + this.thePainter.formatTime((int)this.nextLength));

        while ((System.currentTimeMillis() < l) && (this.currentlyBreaking == true)) {
          sleep(1000);
        }
        this.currentlyBreaking = false;
        WhiteBearAIOMagic.Antiban.access$300(this.antiban);
        return 10;
      }

      if (!this.thePainter.savedStats) {
        this.startLoc = getMyPlayer().getLocation();
        this.camera.setPitch(true);
        this.thePainter.saveStats();
        return 100;
      }

      this.interfaces.getComponent(741, 9).doClick();
      this.interfaces.getComponent(620, 18).doClick();
      this.interfaces.getComponent(109, 13).doClick();
      this.interfaces.getComponent(335, 19).doClick();
      Object localObject;
      if (!this.foundType) {
        this.floor = this.game.getPlane();
        this.thePainter.savedStats = true;
        this.startLoc = getMyPlayer().getLocation();
        if (this.doWhat == 0) {
          localObject = this.grandExchange.lookup(563);
          if (localObject != null)
            this.priceGuide = ((GrandExchange.GEItem)localObject).getGuidePrice();
          else {
            this.priceGuide = 0;
          }
          this.itemtype = "Law Rune";
        } else if (this.doWhat == 5) {
          localObject = this.grandExchange.lookup(this.barID);
          if (localObject != null)
            this.priceGuide = ((GrandExchange.GEItem)localObject).getGuidePrice();
          else {
            this.priceGuide = 0;
          }
          this.itemtype = this.barName;
        } else if (this.doWhat == 8) {
          localObject = this.grandExchange.lookup(this.bolteId);
          if (localObject != null)
            this.priceGuide = ((GrandExchange.GEItem)localObject).getGuidePrice();
          else
            this.priceGuide = 0;
        }
        else {
          localObject = this.grandExchange.lookup(this.itemID);
          if (localObject != null)
            this.priceGuide = ((GrandExchange.GEItem)localObject).getGuidePrice();
          else {
            this.priceGuide = 0;
          }
          this.itemtype = ("Item " + this.itemID);
        }
        this.foundType = true;
      }
      try {
        localObject = WhiteBearAIOMagic.Antiban.access$400(this.antiban);
        if ((localObject != null) && 
          (System.currentTimeMillis() < this.chatRes.nextModAlert)) {
          this.chatRes.nextModAlert += 150000L;
          this.log.warning("[MOD] There is a Moderator nearby! Name: " + ((RSPlayer)localObject).getName());
        }
      }
      catch (Exception localException)
      {
      }

      return doAction(); } catch (Throwable localThrowable) {
    }
    return 100;
  }

  private int doAction()
  {
    int i1;
    int n;
    switch (WhiteBearAIOMagic.2.$SwitchMap$WhiteBearAIOMagic$State[getState().ordinal()]) {
    case 1:
      this.status = "Enchanting Bolts";
      if ((this.useFkeys) && (this.game.getCurrentTab() != 7)) {
        this.keyboard.pressKey('s');
        sleep(random(50, 110));
        this.keyboard.releaseKey('s');
        sleep(random(160, 220));
      }
      if (!this.interfaces.get(432).isValid()) {
        this.magic.castSpell(this.component);
        sleep(random(400, 600));
      }
      if ((this.interfaces.get(432).isValid()) && 
        (!this.interfaces.getComponent(432, this.boltComponent).doAction("Make 10")))
      {
        sleep(random(300, 400));
        if (!this.interfaces.getComponent(432, this.boltComponent).doAction("Make 10"))
        {
          sleep(random(300, 400));
          if ((this.interfaces.get(432).isValid()) && (!this.interfaces.getComponent(432, this.boltComponent).doAction("Make 10")))
          {
            sleep(random(300, 400));
            if (!this.interfaces.getComponent(432, this.boltComponent).doAction("Make 10"))
            {
              sleep(random(300, 400));
              this.interfaces.getComponent(432, 12).doClick();
              this.log.warning("Unable to cast Enchant Crossbow Bolt spell for 4 tries, logging out.");
              this.logOutR = true;
              return random(200, 300);
            }
          }
        }
      }

      if ((random(0, 4) == 0) && 
        (WhiteBearAIOMagic.Antiban.access$500(this.antiban))) {
        return random(50, 100);
      }

      if (random(0, 3) == 0) {
        WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
        sleep(random(190, 300));
      } else {
        sleep(random(460, 550));
      }
      int i = 0;
      while ((valid()) && (getMyPlayer().getAnimation() == -1) && (i < 26)) {
        i++;
        sleep(random(49, 52));
      }
      i = 0;
      while ((valid()) && (getMyPlayer().getAnimation() != -1) && (i < 46)) {
        i++;
        sleep(random(49, 52));
        if ((getMyPlayer().getAnimation() <= 4000) || (getMyPlayer().getAnimation() >= 5000))
          continue;
        i = 20;
      }
      return random(20, 50);
    case 2:
      this.status = ("Cast " + this.spellName);
      if ((this.useFkeys) && (this.game.getCurrentTab() != 7)) {
        this.keyboard.pressKey('s');
        sleep(random(50, 110));
        this.keyboard.releaseKey('s');
        sleep(random(160, 220));
      }
      this.magic.castSpell(this.component);
      if ((random(0, 4) == 0) && 
        (WhiteBearAIOMagic.Antiban.access$500(this.antiban))) {
        return random(50, 100);
      }

      if (random(0, 3) == 0) {
        WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
        sleep(random(190, 300));
      } else {
        sleep(random(460, 550));
      }
      int j = 0;
      while ((valid()) && (getMyPlayer().getAnimation() == 8939) && (j < 46)) {
        j++;
        sleep(random(49, 52));
      }
      return 20;
    case 3:
      this.status = ("Cast " + this.spellName);
      if ((this.clicked) || (!this.magic.isSpellSelected())) {
        if ((this.useFkeys) && (this.game.getCurrentTab() != 7)) {
          this.keyboard.pressKey('s');
          sleep(random(50, 110));
          this.keyboard.releaseKey('s');
          sleep(random(160, 220));
        }
        this.magic.castSpell(this.component);
        this.clicked = false;
      }
      if (this.magic.isSpellSelected()) {
        if ((this.useFkeys) && (this.game.getCurrentTab() != 4)) {
          this.keyboard.pressKey('p');
          sleep(random(50, 110));
          this.keyboard.releaseKey('p');
          sleep(random(160, 220));
        }
        for (int k = 0; k < 11; k++) {
          sleep(50);
          if (this.game.getCurrentTab() == 4)
            break;
        }
        if (this.inventory.getItem(new int[] { this.itemID }) == null) {
          sleep(random(150, 250));
          if (this.inventory.getItem(new int[] { this.itemID }) == null) {
            sleep(random(150, 250));
            if (this.inventory.getItem(new int[] { this.itemID }) == null) {
              sleep(random(800, 1100));
              if (this.inventory.getItem(new int[] { this.itemID }) == null) {
                RSItem[] arrayOfRSItem1 = this.equipment.getItems();
                for (RSItem localRSItem : arrayOfRSItem1) {
                  if (localRSItem.getID() == this.itemID) {
                    localRSItem.doAction("Remove");
                    return 100;
                  }
                }
                this.log.warning("You have run out of the selected item (" + this.itemID + ")! Logging out!");

                doLogOut(false, true);
                return 100;
              }
            }
          }
        }
        if (this.inventory.getItem(new int[] { this.itemID }).doAction("Cast"))
          this.clicked = true;
        if ((random(0, 4) == 0) && 
          (WhiteBearAIOMagic.Antiban.access$500(this.antiban))) {
          return random(50, 100);
        }

        if (random(0, 3) == 0) {
          WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
          sleep(random(190, 300));
        } else {
          sleep(random(460, 550));
        }
        sleep(500);
        int m = 0;
        while ((valid()) && (this.game.getCurrentTab() != 7) && (m <= 23)) {
          m++;
          sleep(random(99, 102));
        }
      }
      return 10;
    case 4:
      if (this.curseWait < System.currentTimeMillis()) {
        this.status = ("Cast " + this.spellName);
        if (!this.magic.isSpellSelected()) {
          if ((this.useFkeys) && (this.game.getCurrentTab() != 7)) {
            this.keyboard.pressKey('s');
            sleep(random(50, 110));
            this.keyboard.releaseKey('s');
            sleep(random(160, 220));
          }
          this.magic.castSpell(this.component);
        }
        if (this.magic.isSpellSelected()) {
          RSNPC localRSNPC1 = this.npcs.getNearest(new int[] { this.npcID });
          if ((localRSNPC1 != null) && (localRSNPC1.isOnScreen())) {
            if (!localRSNPC1.doAction("Cast"))
              WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
            else {
              sleep(500);
            }
            i1 = 0;

            while ((valid()) && (getMyPlayer().getAnimation() != -1) && (i1 <= 20)) {
              i1++;
              sleep(random(99, 102));
            }
          } else {
            this.status = "Waiting";
            if ((random(0, 4) == 0) && 
              (WhiteBearAIOMagic.Antiban.access$700(this.antiban))) {
              return random(50, 100);
            }

            if (random(0, 3) == 0) {
              WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
              sleep(random(190, 300));
            } else {
              sleep(random(460, 550));
            }
          }
        }
      } else {
        this.status = "Waiting";
        if ((random(0, 4) == 0) && 
          (WhiteBearAIOMagic.Antiban.access$700(this.antiban))) {
          return random(50, 100);
        }

        if (random(0, 3) == 0) {
          WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
          sleep(random(190, 300));
        } else {
          sleep(random(460, 550));
        }
      }
      return 200;
    case 5:
      if (this.doBanking) {
        this.status = "Banking";
        openBank();
        sleep(random(400, 450));
        if (this.bank.isOpen()) {
          if ((this.bank.getItem(this.ore1ID) == null) || (this.bank.getItem(this.ore2ID) == null))
          {
            sleep(random(400, 600));
            if (this.bank.getItem(995) == null) {
              this.bank.close();
              return random(600, 800);
            }
            if ((this.bank.getItem(this.ore1ID) == null) || (this.bank.getItem(this.ore2ID) == null))
            {
              sleep(random(600, 800));
              if ((this.bank.getItem(this.ore1ID) == null) || (this.bank.getItem(this.ore2ID) == null))
              {
                this.log.warning("You have run out of the ores required! Logging out!");
                doLogOut(false, true);
                return 100;
              }
            }
          }
          if ((this.inventory.contains(this.barID)) && (!this.bank.deposit(this.barID, 0))) {
            return 100;
          }
          sleep(random(500, 550));
          n = this.inventory.getCount(new int[] { this.ore1ID }); i1 = this.inventory.getCount(new int[] { this.ore2ID });

          if (this.ore2ID == -1) {
            this.bank.withdraw(this.ore1ID, 0);
          } else {
            if ((n > 0) && (n != this.ore1Amt))
              this.bank.deposit(this.ore1ID, 0);
            sleep(random(90, 150));
            if ((i1 > 0) && (i1 != this.ore2Amt))
              this.bank.deposit(this.ore2ID, 0);
            sleep(random(300, 600));
            this.bank.withdraw(this.ore2ID, this.ore2Amt);
            sleep(random(90, 150));
            this.bank.withdraw(this.ore1ID, this.ore1Amt);
          }
          this.doBanking = false;
          this.bankCount = this.bank.getCount(new int[] { this.ore1ID });
          sleep(random(450, 600));
          this.bank.close();
          return 10;
        }
      } else {
        this.status = "Casting Superheat";
        if (this.bank.isOpen()) {
          this.bank.close();
        }
        if ((this.useFkeys) && (this.game.getCurrentTab() != 7)) {
          this.keyboard.pressKey('s');
          sleep(random(50, 110));
          this.keyboard.releaseKey('s');
          sleep(random(160, 220));
        }
        if (!this.magic.castSpell(this.component)) {
          return random(200, 500);
        }
        n = 0;

        while ((valid()) && (n < 24) && (this.game.getCurrentTab() != 4)) {
          n++;
          sleep(50);
        }
        if (!findOre()) {
          this.doBanking = true;
          return random(100, 200);
        }
        if (!getLastOre().doAction("Cast Superheat")) {
          return random(400, 600);
        }
        if ((random(0, 4) == 0) && 
          (WhiteBearAIOMagic.Antiban.access$500(this.antiban))) {
          return random(50, 100);
        }
        if (random(0, 3) == 0)
          WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
        sleep(random(350, 550));
        if ((this.useFkeys) && (this.game.getCurrentTab() != 7)) {
          this.keyboard.pressKey('s');
          sleep(random(50, 110));
          this.keyboard.releaseKey('s');
          sleep(random(160, 220));
        }
        i1 = 0;

        while ((valid()) && (this.game.getCurrentTab() == 4) && (i1 < 24)) {
          i1++;
          sleep(50);
        }
      }

      return 200;
    case 6:
      if (this.doBanking) {
        this.status = "Banking";
        openBank();
        sleep(random(400, 450));
        if (this.bank.isOpen()) {
          if (this.bank.getItem(this.itemID) == null) {
            sleep(random(400, 600));
            if ((this.bank.getItem(995) == null) && (this.bank.getItem(this.foodItem) == null))
            {
              this.bank.close();
              return random(600, 800);
            }
            if (this.bank.getItem(this.itemID) == null) {
              sleep(random(600, 800));
              if (this.bank.getItem(this.itemID) == null) {
                this.log.warning("You have run out of bones! Logging out!");
                doLogOut(false, true);
                return 100;
              }
            }
          }
          if ((this.inventory.isFull()) && (this.inventory.contains(this.itemID))) {
            this.doBanking = false;
            return random(100, 300);
          }
          if (this.inventory.contains(this.foodItem)) {
            if (this.bank.deposit(this.foodItem, 0)) {
              sleep(random(500, 600));
              if (this.bank.withdraw(this.itemID, 0)) {
                this.doBanking = false;
                this.bankCount = this.bank.getCount(new int[] { this.itemID });
                sleep(random(150, 250));
                this.bank.close();
              }
            }
          } else if (this.bank.withdraw(this.itemID, 0)) {
            this.doBanking = false;
            this.bankCount = this.bank.getCount(new int[] { this.itemID });
            sleep(random(150, 250));
            this.bank.close();
          }
        }
        return 10;
      }
      if (this.bank.isOpen()) {
        this.bank.close();
      }
      this.status = ("Casting " + this.spellName);
      if ((this.useFkeys) && (this.game.getCurrentTab() != 7)) {
        this.keyboard.pressKey('s');
        sleep(random(50, 110));
        this.keyboard.releaseKey('s');
        sleep(random(160, 220));
      }
      if (!this.magic.castSpell(this.component)) {
        return random(500, 600);
      }
      if ((random(0, 4) == 0) && 
        (WhiteBearAIOMagic.Antiban.access$500(this.antiban))) {
        return random(50, 100);
      }

      if (random(0, 3) == 0) {
        WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
        sleep(random(150, 250));
      } else {
        sleep(random(350, 400));
      }
      n = 0;
      while ((valid()) && (n < 11)) {
        n++;
        sleep(50);
        if (this.doBanking) {
          return 1;
        }
      }
      return random(140, 230);
    case 7:
      int[] arrayOfInt = { 561, 554, 564, 558, 555, 557, 556, this.itemID };
      if (this.doBanking) {
        this.status = "Banking";
        openBank();
        if (this.bank.isOpen()) {
          sleep(random(500, 750));
          this.bankCount = this.bank.getCount(new int[] { this.itemID });
          if ((this.inventory.isFull()) && (this.inventory.contains(this.itemID))) {
            this.doBanking = false;
            return random(100, 300);
          }
          if (!this.bank.depositAllExcept(arrayOfInt)) {
            return random(500, 600);
          }
          sleep(random(500, 600));
          if (this.bank.getItem(this.itemID) == null) {
            sleep(random(400, 600));
            if (this.bank.getItem(995) == null) {
              this.bank.close();
              return random(600, 800);
            }
            if (this.bank.getItem(this.itemID) == null) {
              sleep(random(600, 800));
              if (this.bank.getItem(this.itemID) == null) {
                this.log.warning("You have run out of the selected item (" + this.itemID + ")! Logging out!");

                doLogOut(false, true);
                return 100;
              }
            }
          }
          if (this.bank.withdraw(this.itemID, 0)) {
            sleep(random(100, 200));
            this.bankCount = this.bank.getCount(new int[] { this.itemID });
            this.doBanking = false;
            this.bank.close();
          }
        }
        return 10;
      }
      this.bank.close();
      this.status = ("Casting Lvl-" + this.spellName);
      if (!this.magic.isSpellSelected()) {
        if ((this.useFkeys) && (this.game.getCurrentTab() != 7)) {
          this.keyboard.pressKey('s');
          sleep(random(50, 110));
          this.keyboard.releaseKey('s');
          sleep(random(160, 220));
        }
        if (this.magic.castSpell(this.component)) {
          i1 = 0;

          while ((valid()) && (this.game.getCurrentTab() != 4) && (i1 < 20)) {
            i1++;
            sleep(50);
          }
        }
      }
      if (this.magic.isSpellSelected()) {
        if ((this.useFkeys) && (this.game.getCurrentTab() != 4)) {
          this.keyboard.pressKey('p');
          sleep(random(50, 110));
          this.keyboard.releaseKey('p');
          sleep(random(160, 220));
        }
        if (this.inventory.getItem(new int[] { this.itemID }) == null) {
          this.doBanking = true;
          return 10;
        }
        if (!this.inventory.getItem(new int[] { this.itemID }).doAction("Cast Lvl-" + this.spellName))
        {
          return random(300, 500);
        }
        if ((random(0, 4) == 0) && 
          (WhiteBearAIOMagic.Antiban.access$500(this.antiban))) {
          return random(50, 100);
        }

        if (random(0, 3) == 0) {
          WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
          sleep(random(150, 250));
        } else {
          sleep(random(350, 400));
        }
        i1 = 0;

        while ((valid()) && (this.game.getCurrentTab() == 4) && (i1 < 20)) {
          i1++;
          sleep(50);
        }
        if (this.inventory.getItem(new int[] { this.itemID }) == null) {
          this.doBanking = true;
          return 10;
        }

      }

      return random(10, 90);
    case 8:
      this.status = "Doing Alch + Curse";
      if ((this.useFkeys) && (this.game.getCurrentTab() != 7)) {
        this.keyboard.pressKey('s');
        sleep(random(50, 110));
        this.keyboard.releaseKey('s');
        sleep(random(160, 220));
      }
      RSNPC localRSNPC2 = this.npcs.getNearest(new int[] { this.npcID });
      if ((localRSNPC2 != null) && (localRSNPC2.isOnScreen())) {
        this.magic.castSpell(this.curseComponent);
        sleep(random(130, 200));
        if ((this.magic.isSpellSelected()) && 
          (!localRSNPC2.doAction("Cast"))) {
          WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
        }

      }
      else if (WhiteBearAIOMagic.Antiban.access$700(this.antiban)) {
        return random(50, 100);
      }

      this.magic.castSpell(this.component);
      sleep(random(150, 280));
      if (this.magic.isSpellSelected()) {
        if (this.inventory.getItem(new int[] { this.itemID }) == null) {
          sleep(random(300, 500));
          if (this.inventory.getItem(new int[] { this.itemID }) == null) {
            sleep(random(400, 600));
            if (this.inventory.getItem(new int[] { this.itemID }) == null) {
              this.log.warning("You have run out of the selected item (" + this.itemID + ")! Logging out!");

              doLogOut(false, true);
              return 100;
            }
          }
        }
        if (this.inventory.getItem(new int[] { this.itemID }).doAction("Cast")) {
          if (random(0, 4) == 0) {
            WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
            sleep(random(150, 250));
          } else {
            sleep(random(350, 400));
          }
          ??? = 0;

          while ((valid()) && (this.game.getCurrentTab() == 4) && (??? <= 30)) {
            ???++;
            sleep(random(49, 52));
          }
        }
      }
      return random(100, 230);
    case 9:
      if (this.invFull) {
        if ((this.useFkeys) && (this.game.getCurrentTab() != 4)) {
          this.keyboard.pressKey('p');
          sleep(random(50, 110));
          this.keyboard.releaseKey('p');
          sleep(random(160, 220));
        }
        for (??? = 0; ??? < 28; ???++) {
          if (this.interfaces.getComponent(149, 0).getComponent(???).getComponentID() == this.itemID)
          {
            if (this.interfaces.getComponent(149, 0).getComponent(???).doAction("Drop"))
            {
              sleep(random(300, 500));
            }
          }
          if (!valid())
            break;
        }
      }
      RSGroundItem localRSGroundItem = this.groundItems.getNearest(new int[] { this.itemID });
      if ((localRSGroundItem == null) || (!localRSGroundItem.isOnScreen())) {
        this.status = "Waiting";
        if (!WhiteBearAIOMagic.Antiban.access$800(this.antiban)) {
          WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
        }
        return random(56, 80);
      }
      if ((this.useFkeys) && (this.game.getCurrentTab() != 7)) {
        this.keyboard.pressKey('s');
        sleep(random(50, 110));
        this.keyboard.releaseKey('s');
        sleep(random(160, 220));
      }
      this.status = "Telegrabing";
      this.magic.castSpell(this.component);
      localRSGroundItem.doAction("Cast");
      sleep(random(500, 600));
      ??? = 0;

      while ((valid()) && (getMyPlayer().getAnimation() != -1) && (??? <= 30)) {
        ???++;
        sleep(random(49, 52));
      }
      return 10;
    case 10:
      WhiteBearAIOMagic.Antiban.access$600(this.antiban, true);
      return 100;
    }
    return 100;
  }

  private WhiteBearAIOMagic.State getState() {
    if ((this.doWhat != 0) && (this.calc.distanceTo(this.startLoc) > 2)) {
      if (!onTile(this.startLoc, "Walk here", 0.5D, 0.5D, 0)) {
        this.walking.walkTileMM(this.startLoc);
      }
      sleep(random(800, 1200));
    }
    if (((this.doWhat == 2) || (this.doWhat == 3)) && (this.game.getPlane() != this.floor)) {
      this.log.warning("Player is no longer on plane " + this.floor + "! Logging out");

      this.logOutR = true;
      return WhiteBearAIOMagic.State.Antiban;
    }
    switch (this.doWhat) {
    case 0:
      return WhiteBearAIOMagic.State.Teleport;
    case 1:
      return WhiteBearAIOMagic.State.Alchemy;
    case 2:
      return WhiteBearAIOMagic.State.Curse;
    case 3:
      return WhiteBearAIOMagic.State.AlchNCurse;
    case 4:
      return WhiteBearAIOMagic.State.Enchant;
    case 5:
      return WhiteBearAIOMagic.State.Superheat;
    case 6:
      return WhiteBearAIOMagic.State.Bones;
    case 7:
      return WhiteBearAIOMagic.State.Telekinetic;
    case 8:
      return WhiteBearAIOMagic.State.Bolts;
    }
    this.status = "Error!";
    return WhiteBearAIOMagic.State.Antiban;
  }

  public boolean onStart()
  {
    this.chatRes = new WhiteBearAIOMagic.ChatResponder(this, null);
    this.chatRes.start();
    this.env.disableRandom("Login");
    return true;
  }

  public void onFinish()
  {
    this.counter = 405;
    this.chatRes.run = false;
    if ((this.game.isLoggedIn()) && (this.exitStage < 2) && (this.thePainter.runTime >= 3600000L)) {
      this.env.saveScreenshot(false);
    }
    this.log.info("In just " + this.totalTime + ", you gained " + this.totalExp + " magic exp.");

    this.log.info("Thank you for using White Bear AIO Magic!");
  }

  public void onRepaint(Graphics paramGraphics)
  {
    if ((this.foundType) && 
      (this.pastExp != this.skills.getCurrentExp(6))) {
      this.count1 += 1;
      this.expGained = (this.count1 * this.exp);
    }

    this.pastExp = this.skills.getCurrentExp(6);
    try
    {
      if (this.game.isLoggedIn()) {
        localObject = new Rectangle(this.interfaces.get(137).getComponent(54).getAbsoluteX(), this.interfaces.get(137).getComponent(54).getAbsoluteY() + 2, 89, 13);

        paramGraphics.setColor(new Color(211, 192, 155, 253));
        try {
          paramGraphics.fillRect(((Rectangle)localObject).x, ((Rectangle)localObject).y, ((Rectangle)localObject).width, ((Rectangle)localObject).height);
        }
        catch (Exception localException2) {
        }
      }
      paramGraphics.setFont(new Font("sansserif", 0, 12));
      if (this.thePainter.antialias == true) {
        ((Graphics2D)paramGraphics).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        ((Graphics2D)paramGraphics).setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);

        ((Graphics2D)paramGraphics).setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
      }

      if (this.currentlyBreaking) {
        paramGraphics.setColor(new Color(0, 0, 0));
        paramGraphics.setColor(Color.BLACK);
        paramGraphics.setFont(new Font("sansserif", 1, 14));
        paramGraphics.drawString("Currently taking a break!", 10, 30);
      }

      Object localObject = this.mouse.getLocation();
      int i = (int)((Point)localObject).getX();
      int j = (int)((Point)localObject).getY();
      if (System.currentTimeMillis() - this.mouse.getPressTime() < 900L)
        paramGraphics.setColor(new Color(0, 0, 255, 170));
      else
        paramGraphics.setColor(new Color(0, 0, 255, 75));
      paramGraphics.drawLine(i, 0, i, this.game.getHeight());
      paramGraphics.drawLine(0, j, this.game.getWidth(), j);
      paramGraphics.fillRect(i - 1, j - 1, 3, 3);
    } catch (Throwable localThrowable) {
    }
    try {
      if ((this.thePainter.savedStats == true) && (this.game.getClientState() == 10))
        this.thePainter.paint(paramGraphics);
    }
    catch (Exception localException1)
    {
    }
  }

  public void messageReceived(MessageEvent paramMessageEvent)
  {
    try
    {
      String str = paramMessageEvent.getMessage();
      if ((paramMessageEvent.getID() > 0) && (paramMessageEvent.getID() < 10))
        return;
      if (str.contains("You've just advanced")) {
        if (this.lvlAmt == 0) {
          log("[Alert] You have just leveled, thanks to White Bear AIO Magic!");
        }
        else if (random(1, 3) == 1)
          log("[Alert] Another level by White Bear AIO Magic!");
        else {
          log("[Alert] Congratulations! You have just leveled!");
        }

        this.lvlAmt += 1;
      }
      if (str.contains("Oh dear")) {
        this.log.severe("[Alert] You were killed! Aborting script!");
        this.logOutR = true;
      }
      if (str.contains("wishes to trade with you")) {
        this.tradeResponse = true;
      }
      if ((str.contains("System update in")) && (!str.contains(":")))
      {
        this.log.warning("There will be a system update soon, so we logged out");
        this.logOutR = true;
      }
      if ((str.contains("holding any bones")) && (!str.contains(":")))
      {
        this.doBanking = true;
      }
      if ((str.contains("inventory space to hold that item")) && (!str.contains(":")))
      {
        this.invFull = true;
      }
      if ((str.contains("You do not have enough")) && (!str.contains(":")) && (str.contains("cast this spell")))
      {
        this.log.severe("[Alert] You ran out of runes! Logging out!");
        this.logOutR = true;
      }
      if (str.contains("don't have enough runes for that")) {
        this.log.severe("[Alert] You ran out of runes! Logging out!");
        this.logOutR = true;
      }
      if ((str.contains("Your Magic level is not high enough")) && (!str.contains(":")))
      {
        this.log.severe("[Alert] You cannot use that spell! Logging out!");
        this.logOutR = true;
      }
      if ((str.contains("has already been")) && (str.contains("Your foe")) && (!str.contains(":")))
      {
        this.curseWait = (System.currentTimeMillis() + 5000L);
      }
      if ((str.contains("currently immune to that spell")) && (!str.contains(":")))
      {
        this.curseWait = (System.currentTimeMillis() + 5000L);
      }
    }
    catch (Throwable localThrowable)
    {
    }
  }

  private void activate()
  {
    if (!this.game.isFixed()) {
      this.log.warning("Your screen size is not Fixed!");
      this.log.warning("The script will encounter problems if you don't change it to fixed!");
    }
    SwingUtilities.invokeLater(new WhiteBearAIOMagic.1(this));

    while (!this.guiStart) {
      sleep(100);
    }
    this.thePainter.setColour();
    if (this.stopTime > 0L) {
      this.log.info("Script will stop after " + this.thePainter.formatTime((int)this.stopTime));

      long l = this.stopTime + System.currentTimeMillis();
      this.stopTime = l;
    }
    if (this.checkUpdates) {
      URLConnection localURLConnection = null;
      BufferedReader localBufferedReader = null;
      try {
        localURLConnection = new URL("http://whitebearrs.orgfree.com/content/updater.php?script=magic&type=all").openConnection();

        localBufferedReader = new BufferedReader(new InputStreamReader(localURLConnection.getInputStream()));

        String str1 = localBufferedReader.readLine();
        String str2 = localBufferedReader.readLine();
        String str3 = localBufferedReader.readLine();
        if (Double.parseDouble(str1) > this.properties.version()) {
          this.log.warning("Latest version: " + str1 + "! Please update the script!");

          this.log.info("In this update: " + str3);
          this.log.info("Download from: " + str2);
        } else if (Double.parseDouble(str1) < this.properties.version()) {
          this.log.info("You are using a beta version of this script!");
        } else {
          this.log.info("You are using the latest version of this script!");
        }
        if (localBufferedReader != null)
          localBufferedReader.close();
      } catch (Exception localException) {
        this.log.warning("An error occurred while checking for update!");
      }
    }
    WhiteBearAIOMagic.Antiban.access$300(this.antiban);
    boolean bool = WhiteBearAIOMagic.Antiban.access$11000(this.antiban);
    while (!bool) {
      bool = WhiteBearAIOMagic.Antiban.access$11000(this.antiban);
    }
    if (!this.game.isLoggedIn()) {
      this.log.warning("You should start the script logged in!");
    }
    this.thePainter.time_ScriptStart = System.currentTimeMillis();
  }

  private boolean openBank() {
    int i = 0;
    try {
      if (!this.bank.isOpen())
      {
        Object localObject;
        if (this.bankMode == 1) {
          localObject = this.npcs.getNearest(Bank.BANKERS);
          if (onTile(((RSNPC)localObject).getLocation(), "Bank Banker", 0.5D, 0.5D, 0)) {
            sleep(800);
            while ((!this.bank.isOpen()) && (i < 7)) {
              sleep(random(90, 110));
              if (getMyPlayer().isMoving())
                i = 0;
              i++;
            }
          }
          return false;
        }
        else if (this.bankMode == 2) {
          localObject = this.objects.getNearest(Bank.BANK_BOOTHS);
          if (onTile(((RSObject)localObject).getLocation(), "Use-quickly", 0.5D, 0.5D, 0)) {
            sleep(800);
            while ((!this.bank.isOpen()) && (i < 7)) {
              sleep(random(90, 110));
              if (getMyPlayer().isMoving())
                i = 0;
              i++;
            }
          }
          return false;
        }
        else {
          localObject = this.objects.getNearest(Bank.BANK_CHESTS);
          if (((RSObject)localObject).doAction(this.bankMode == 3 ? "Bank" : "Use")) {
            sleep(800);
            while ((!this.bank.isOpen()) && (i < 7)) {
              sleep(random(90, 110));
              if (getMyPlayer().isMoving())
                i = 0;
              i++;
            }
          }
          return false;
        }
      }

      return true; } catch (Exception localException) {
    }
    return false;
  }

  private boolean doLogOut(boolean paramBoolean1, boolean paramBoolean2)
  {
    this.status = "Logging out";
    while (this.bank.isOpen()) {
      this.bank.close();
      this.mouse.move(random(10, 430), random(10, 465));
      sleep(random(200, 400));
    }
    while (!this.game.isOnLogoutTab()) {
      this.mouse.move(random(this.game.getWidth() - 15, this.game.getWidth() - 5), random(5, 16));

      this.mouse.click(true);
      if (this.bank.isOpen()) {
        this.bank.close();
      }
      i = 0;
      while ((!this.game.isOnLogoutTab()) && (i < 5)) {
        sleep(random(200, 400));
        i++;
      }
    }
    int i = 0;
    while ((this.game.isLoggedIn() == true) && (i < 20)) {
      if (paramBoolean1)
        this.interfaces.get(182).getComponent(2).doClick();
      else {
        this.interfaces.get(182).getComponent(6).doClick();
      }
      sleep(1000);
    }
    if ((!paramBoolean1) && (paramBoolean2)) {
      stopScript(false);
    }
    return true;
  }

  private boolean findOre() {
    if (this.ore2ID > 0) {
      return (this.inventory.contains(this.ore1ID)) && (this.inventory.contains(this.ore2ID));
    }
    return this.inventory.contains(this.ore1ID);
  }

  private RSComponent getLastOre() {
    for (int i = 27; i >= 0; i--) {
      if (this.interfaces.getComponent(149, 0).getComponent(i).getComponentID() == this.ore1ID)
      {
        return this.interfaces.getComponent(149, 0).getComponent(i);
      }
    }
    return null;
  }

  private boolean onTile(RSTile paramRSTile, String paramString, double paramDouble1, double paramDouble2, int paramInt) {
    Point localPoint;
    try {
      localPoint = this.calc.tileToScreen(paramRSTile, paramDouble1, paramDouble2, paramInt);
      if (!this.calc.pointOnScreen(localPoint)) {
        if (this.calc.distanceTo(paramRSTile) <= 8) {
          if (getMyPlayer().isMoving()) {
            return false;
          }
          this.walking.walkTileMM(paramRSTile);
          this.walking.sleep(1000);
          return false;
        }
        return false;
      }
    } catch (Exception localException1) {
    }
    try {
      int i = 0;
      for (int j = 0; j <= 50; j++) {
        localPoint = this.calc.tileToScreen(paramRSTile, paramDouble1, paramDouble2, paramInt);
        if (!this.calc.pointOnScreen(localPoint)) {
          return false;
        }
        this.mouse.move(localPoint);
        String[] arrayOfString = this.menu.getItems();
        for (int k = 0; k < arrayOfString.length; k++) {
          if (!arrayOfString[k].toString().toLowerCase().contains(paramString.toLowerCase()))
            continue;
          i = 1;
          break;
        }

        if (i != 0)
          break;
      }
    }
    catch (Exception localException2) {
    }
    try {
      return this.menu.doAction(paramString);
    } catch (Exception localException3) {
    }
    return false;
  }

  private boolean valid() {
    return this.game.isLoggedIn();
  }

  public void mouseClicked(MouseEvent paramMouseEvent)
  {
  }

  public void mouseEntered(MouseEvent paramMouseEvent)
  {
  }

  public void mouseExited(MouseEvent paramMouseEvent)
  {
  }

  public void mouseReleased(MouseEvent paramMouseEvent)
  {
  }

  public void mouseMoved(MouseEvent paramMouseEvent)
  {
    this.thePainter.p = paramMouseEvent.getPoint();
  }

  public void mouseDragged(MouseEvent paramMouseEvent)
  {
    Point localPoint = paramMouseEvent.getPoint();
    processPaint(localPoint);
  }

  public void mousePressed(MouseEvent paramMouseEvent)
  {
    Point localPoint = paramMouseEvent.getPoint();
    processPaint(localPoint);

    if ((this.thePainter.logOutYes.contains(localPoint)) && (this.exitStage == 1)) {
      this.exitStage = 2;
      if (!this.logOutInfo) {
        log("You will be logged out when the current loop ends (i.e. in a while)");
        this.logOutInfo = true;
      }
    }
    if ((this.thePainter.logOutNo.contains(localPoint)) && (this.exitStage == 1)) {
      this.exitStage = 0;
    }
    if ((this.thePainter.logOut.contains(localPoint)) && (this.exitStage == 0) && (this.thePainter.currentTab == 4))
    {
      this.thePainter.currentTab = 0;
      this.exitStage = 1;
    }
    if ((this.thePainter.currentTab == 4) && (this.counter == 0) && (new Rectangle(this.thePainter.logOut.x + 125, this.thePainter.logOut.y, this.thePainter.logOut.width + 53, this.thePainter.logOut.height).contains(localPoint)))
    {
      this.thePainter.currentTab = 0;
      this.counter = 400;
    }
  }

  private void processPaint(Point paramPoint) {
    Point localPoint = paramPoint;
    int i = 408; int j = 135; int k = 114;
    int m = localPoint.x;
    int n = localPoint.y;
    if ((this.game.getClientState() == 10) && (m >= this.thePainter.paintX) && (m <= this.thePainter.paintX + i) && (n >= this.thePainter.paintY) && (n <= this.thePainter.paintY + k))
    {
      if ((this.thePainter.currentTab != -1) && (this.thePainter.currentTab != 4)) {
        this.thePainter.paintX = (m - i / 2);
        this.thePainter.paintY = (n - j / 2);
      }
    }
    if (this.thePainter.paintX < 4)
      this.thePainter.paintX = 4;
    if (this.thePainter.paintY < 4)
      this.thePainter.paintY = 4;
    if (this.thePainter.paintX + i > 761)
      this.thePainter.paintX = (761 - i);
    if (this.thePainter.paintY + j > 494)
      this.thePainter.paintY = (494 - j);
  }
}